// src/utils/locationService.js
import Geolocation from 'react-native-geolocation-service';

// Mapping of countries to preferred location services
const COUNTRY_LOCATION_SERVICE_MAP = {
  'China': 'AMap',
  '中国': 'AMap',
  // Add more mappings as needed
};

// Countries where Google Maps should be used
const GOOGLE_MAPS_COUNTRIES = [
  'Japan',
  '日本',
  'South Korea',
  '韩国', 
  'Russia',
  '俄罗斯',
  'Ukraine',
  '乌克兰',
  // Add Southeast Asian countries
  'Thailand',
  '泰国',
  'Vietnam',
  '越南',
  'Indonesia',
  '印尼',
  'Malaysia',
  '马来西亚',
  'Philippines',
  '菲律宾',
  'Singapore',
  '新加坡',
  // European countries
  'United Kingdom',
  '英国',
  'Germany',
  '德国',
  'France',
  '法国',
  'Italy',
  '意大利',
  'Spain',
  '西班牙',
  'Netherlands',
  '荷兰',
  'Sweden',
  '瑞典',
  'Norway',
  '挪威',
  'Denmark',
  '丹麦',
  'Finland',
  '芬兰'
];

// Populate the country to service map
GOOGLE_MAPS_COUNTRIES.forEach(country => {
  COUNTRY_LOCATION_SERVICE_MAP[country] = 'GoogleMaps';
});

// Define login options by region
const LOGIN_OPTIONS_BY_REGION = {
  'China': ['phone', 'wechat'],
  'Japan': ['phone', 'apple', 'google'],
  'South Korea': ['phone', 'kakao', 'google', 'apple'],
  'Russia': ['phone', 'vk', 'google'],
  'Global': ['phone', 'google', 'facebook'],
  'Southeast Asia': ['phone', 'google', 'facebook'],
  'Europe': ['phone', 'google', 'apple', 'facebook'],
  'Ukraine': ['phone', 'google', 'facebook']
};

/**
 * Determine which location service to use based on country
 * @param {string} country - Country name
 * @returns {string} Location service to use ('AMap' or 'GoogleMaps')
 */
export const getLocationServiceForCountry = (country) => {
  if (!country) return 'GoogleMaps'; // Default to Google Maps
  
  return COUNTRY_LOCATION_SERVICE_MAP[country] || 'GoogleMaps';
};

/**
 * Check if coordinates are likely in China
 * @param {number} latitude - Latitude coordinate
 * @param {number} longitude - Longitude coordinate
 * @returns {boolean} True if coordinates are likely in China
 */
export const isLikelyInChina = (latitude, longitude) => {
  // China approximate bounding box
  // Latitude: ~18° to ~53°
  // Longitude: ~73° to ~135°
  return (
    latitude >= 18 && 
    latitude <= 53 && 
    longitude >= 73 && 
    longitude <= 135
  );
};

/**
 * Determine which location service to use based on coordinates
 * @param {number} latitude - Latitude coordinate
 * @param {number} longitude - Longitude coordinate
 * @returns {string} Location service to use ('AMap' or 'GoogleMaps')
 */
export const getLocationServiceForCoordinates = (latitude, longitude) => {
  if (isLikelyInChina(latitude, longitude)) {
    return 'AMap';
  }
  return 'GoogleMaps';
};

/**
 * Get available login options for a specific country/region
 * @param {string} country - Country name
 * @returns {Array} Array of available login options
 */
export const getLoginOptionsForCountry = (country) => {
  // Map specific countries to regions
  if (country === 'China' || country === '中国') {
    return LOGIN_OPTIONS_BY_REGION['China'];
  }
  
  if (country === 'Japan' || country === '日本') {
    return LOGIN_OPTIONS_BY_REGION['Japan'];
  }
  
  if (country === 'South Korea' || country === '韩国') {
    return LOGIN_OPTIONS_BY_REGION['South Korea'];
  }
  
  if (country === 'Russia' || country === '俄罗斯') {
    return LOGIN_OPTIONS_BY_REGION['Russia'];
  }
  
  if (country === 'Ukraine' || country === '乌克兰') {
    return LOGIN_OPTIONS_BY_REGION['Ukraine'];
  }
  
  // Check for Southeast Asian countries
  const southeastAsiaCountries = [
    'Thailand', '泰国', 'Vietnam', '越南', 'Indonesia', '印尼',
    'Malaysia', '马来西亚', 'Philippines', '菲律宾', 'Singapore', '新加坡'
  ];
  
  if (southeastAsiaCountries.includes(country)) {
    return LOGIN_OPTIONS_BY_REGION['Southeast Asia'];
  }
  
  // Check for European countries
  const europeanCountries = [
    'United Kingdom', '英国', 'Germany', '德国', 'France', '法国',
    'Italy', '意大利', 'Spain', '西班牙', 'Netherlands', '荷兰',
    'Sweden', '瑞典', 'Norway', '挪威', 'Denmark', '丹麦', 'Finland', '芬兰'
  ];
  
  if (europeanCountries.includes(country)) {
    return LOGIN_OPTIONS_BY_REGION['Europe'];
  }
  
  // Default to global options
  return LOGIN_OPTIONS_BY_REGION['Global'];
};